package com.example.service.approval;

import com.example.data.approval.Approval;
import com.example.data.approval.ApprovalBO;
import com.example.repository.ApprovalRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class ApprovalServiceImpl implements ApprovalService{
    private ApprovalRepository approvalRepository;
    @Autowired
    public ApprovalServiceImpl(ApprovalRepository approvalRepository){
        this.approvalRepository = approvalRepository;
    }
    @Override
    public List<Approval> getAllApproval() {
        Approval approvalInfo = ApprovalBO.convert(approvalRepository.getAllApproval().get(0));//?为什么这里有get(0)
        return Arrays.asList(approvalInfo);
    }

    @Override
    public List<Approval> getApprovalByID(Long empID, Long appID) {
        Approval approvalInfo = ApprovalBO.convert(approvalRepository.getApprovalByID(empID, appID));
        return Arrays.asList(approvalInfo);
    }

    @Override
    public List<Approval> addApproval(Approval approval) {
        Approval approvalInfo = ApprovalBO.convert(approvalRepository.addApproval(approval));
        return Arrays.asList(approvalInfo);
    }

    @Override
    public List<Approval> editApproval(Approval approvalVO) {//need to change
        long appID = approvalVO.getAppID();
        Map<String, Object> approvalMap = approvalRepository.queryByAppID(appID);
        Approval approvalBO = new ApprovalBO();
        BOFilled(approvalBO, approvalMap);
        BOUpdate(approvalBO, approvalVO);
        Approval approvalInfo = ApprovalBO.convert(approvalRepository.editApproval(appID,approvalBO));
        return Arrays.asList(approvalInfo);
    }

    @Override
    public void deleteApproval(long appID) {
        approvalRepository.deleteApproval(appID);

    }
    public void BOFilled(Approval approvalBO, Map<String, Object> approvalMap){
        for(Map.Entry<String, Object> entry: approvalMap.entrySet()){
            switch (entry.getKey()){
                case "empID":
                    approvalBO.setEmpID((Long)entry.getValue());
                    break;
                case "type":
                    approvalBO.setType((Integer) entry.getValue());
                    break;
                case "approval":
                    approvalBO.setApproval((Boolean)entry.getValue());
                    break;
                case "comment":
                    approvalBO.setComment((String) entry.getValue());
                    break;
            }
        }
    }
    public void BOUpdate(Approval approvalBO, Approval approvalVO){
        if(approvalVO.getComment() != null){
            approvalBO.setComment(approvalVO.getComment());
        }
        if(approvalVO.getApproval() != null){
            approvalBO.setApproval(approvalVO.getApproval());
        }
        if(approvalVO.getType() != null){
            approvalBO.setType(approvalVO.getType());
        }
        if(approvalVO.getEmpID() != null){
            approvalBO.setEmpID(approvalVO.getEmpID());
        }
    }
}
